from flask import Blueprint,request
import time
from app.utils.sqliteFunc import Sqlite
from app.utils.responseFunc import returnMsg
import os
from werkzeug.utils import secure_filename


mangebp = Blueprint("mange",__name__)
sqlite = Sqlite()


@mangebp.route("/upload",methods=["post"])
def upload():
    f = request.files.get("files")
    filePath = os.getcwd()+'/uploads/'+secure_filename(f.filename)
    f.save(filePath)
    return returnMsg(data=filePath)


@mangebp.route("/banner/list",methods=["post"])
def bannerList():
    sql = "select * from tb_banner where status != 0;"
    data = sqlite.query(sql)
    return returnMsg(data=data)


@mangebp.route("/banner/save",methods=["post"])
def bannerSave():
    body = request.get_json()
    title = body.get("title")
    url = body.get("url")
    remark = body.get("remark")
    sql = "insert into tb_banner (title,url,remark,createTime) values ('{}','{}','{}','{}');".format(title,url,remark,time.strftime("%y-%m-%d %H:%M:%S"))
    sqlite.commit(sql)
    return returnMsg()


@mangebp.route("/banner/edit",methods=["post"])
def bannerEdit():
    content = "欢迎访问"
    return returnMsg()


@mangebp.route("/banner/del",methods=["post"])
def bannerDel():
    body = request.get_json()
    bannerId = body.get("id")
    sql = "update tb_banner set status = 0 where id = {};".format(bannerId)
    sqlite.commit(sql)
    return returnMsg()


@mangebp.route("/category/list",methods=["post"])
def categoryList():
    sql = "select * from tb_category where status != 0;"
    data = sqlite.query(sql)
    return returnMsg(data=data)


@mangebp.route("/category/save",methods=["post"])
def categorySave():
    body = request.get_json()
    category = body.get("category")
    sql = "insert into tb_category (category,createTime) values ('{}','{}');".format(category,time.strftime("%y-%m-%d %H:%M:%S"))
    sqlite.commit(sql)
    return returnMsg()


@mangebp.route("/category/edit",methods=["post"])
def categoryEdit():
    content = "欢迎访问"
    return returnMsg()


@mangebp.route("/category/del",methods=["post"])
def categoryDel():
    body = request.get_json()
    categoryId = body.get("id")
    sql = "update tb_category set status = 0 where id = {};".format(categoryId)
    sqlite.commit(sql)
    return returnMsg()


@mangebp.route("/article/list",methods=["post"])
def articleList():
    sql = "select * from tb_article a join tb_category b on a.categoryId = b.id where a.status != 0;"
    data = sqlite.query(sql)
    return returnMsg(data=data)


@mangebp.route("/article/save",methods=["post"])
def articleSave():
    body = request.get_json()
    categoryId = body.get("categoryId")
    title = body.get("title")
    content = body.get("content")
    sql = "insert into tb_article (categoryId,title,content,createTime) values ({},'{}','{}','{}');".format(categoryId,title,content,time.strftime("%y-%m-%d %H:%M:%S"))
    sqlite.commit(sql)
    return returnMsg()


@mangebp.route("/article/edit",methods=["post"])
def articleEdit():
    content = "欢迎访问"
    return returnMsg()


@mangebp.route("/article/del",methods=["post"])
def articleDel():
    body = request.get_json()
    articleId = body.get("id")
    sql = "update tb_article set status = 0 where id = {};".format(articleId)
    sqlite.commit(sql)
    return returnMsg()


@mangebp.route("/article/comment/del",methods=["post"])
def article_comment_del():
    body = request.get_json()
    commentId = body.get("id")
    sql = "update tb_article_comment set status = 0 where id = {};".format(commentId)
    sqlite.commit(sql)
    return returnMsg()



@mangebp.route("/about/save",methods=["post"])
def aboutMesave():
    body = request.get_json()
    nickName = body.get("nickName")
    email = body.get("email")
    about = body.get("about")
    sql = "update tb_about set nickName='{}',email='{}',about='{}';".format(nickName,email,about)
    sqlite.commit(sql)
    return returnMsg()